import gameConfig from "../../engines/configs/gameConfig";
import O1HD, { managers, modules, services } from "../../engines/gameEngine";
import node_component from "../../engines/libs/node_component";
import { GAME_MODE } from "../../engines/managers/gameContent/gameMode_json";
import { REWARD_TYPE } from "../../engines/services/userContent/userInterface";
import ClickBase from "../Base/ClickBase";
import { PAGE } from "../Enum";
import { IUnlockPagePrams } from "../Interface";
import mainRoot from "../MainRoot";

const { ccclass, property, menu, disallowMultiple } = cc._decorator;

@ccclass
@menu('工具组件/选关按钮')
export default class LevelBtn extends ClickBase {
    private m_levelNum: number = 1;
    private lockBg: cc.Node = null;
    private selcetSp:cc.Node = null;
    private m_numLabel: cc.Label = null;
    private m_mode: GAME_MODE = GAME_MODE.NORMAL;

    public lastBtn: LevelBtn = null;

    public levelObj: any = null;

    private ShowImg: cc.Sprite = null

    private isStartGame: boolean = false;

    private subPowerLb:cc.Label = null;

    private powerNode:cc.Node = null;

    private lb_level:cc.Label = null;

    private levelLayout:cc.Node = null;

    private levelLb:cc.Label = null;

    private imgStr: string = ''
    private get locked() {
        // return this.m_levelNum>mainRoot.LevelSystem.GetFarLevel(this.m_mode);
        return mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) < 0 && !O1HD.getServices().userSrv.getLevelAllUnlockFlag();
    }

    onLoad() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.ClickDown, this);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.ClickUp, this);
        this.node.on(cc.Node.EventType.TOUCH_START, this.OnDown, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.OnUp, this);
        // this.node.on(cc.Node.EventType.TOUCH_END, this.OnUp, this);
        O1HD.on('refrashAll',()=>{
            this.Refresh();
        },this);
    }

    async Init(levelNum: number, mode: GAME_MODE, obj?: any) {
        this.m_mode = mode;
        this.levelObj = obj;
        // console.log("关卡:",obj);
        this.m_levelNum = levelNum;
        this.ShowImg = this.node.getChildByName("Layer").getChildByName("showImg").getComponent(cc.Sprite)
        this.m_numLabel = this.node.getChildByName("Layer").getChildByName("LevelNum").getComponent(cc.Label);
        this.lb_level = this.node.getChildByName("Layer").getChildByName("lbLevel").getComponent(cc.Label);
        this.levelLayout = this.node.getChildByName("level");
        this.levelLb = this.levelLayout.getChildByName("labelLevel").getComponent(cc.Label);
        switch(mode){
            case GAME_MODE.NORMAL:{
                this.levelLb.string = (O1HD.getManagers().peopleArcheryManager.getData().infinityLevel + 1).toString() + "关";
                break;
            }
        }
        // this.lb_level.string = this.m_levelNum.toString();
        this.m_numLabel.string = obj["levelTitle"]; //levelNum.toString();
        this.imgStr = obj["levelImg"];
        if (this.imgStr != null && this.imgStr != '') {
            this.LoadImg();
        }
        this.powerNode = this.node.getChildByName("power");
        this.lockBg = this.node.getChildByName("lockBg");
        this.selcetSp = this.node.getChildByName("select");
        this.subPowerLb = this.powerNode.getChildByName("subPowerLb").getComponent(cc.Label);
        this.subPowerLb.string = `-${obj["subPower"]}`;
        this.levelLayout.active = this.levelObj["infinityFlag"];
        if (this.locked) {
            this.lockBg.active = true;
            this.powerNode.active = false;
            this.levelLayout.active = false;
        }
        let flag2 = this.m_levelNum == mainRoot.LevelSystem.GetFarLevel(this.m_mode)
        if(flag2){
            this.selcetSp.active = true;
        }
        // if(this.m_levelNum == mainRoot.LevelSystem.GetFarLevel(this.m_mode) || (mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum+1)<0 && !this.locked)){
        //     node_component.loadResourcesPicture("Img/Page/LevelMenu/farLevel",this.node,mainRoot.frameBundle);
        // }
    }

    LoadImg() {
        mainRoot.frameBundle.load("Img/Page/LevelMenu/showImg/" + this.imgStr, cc.Texture2D, (e, Ass: cc.Texture2D) => {
            if (e) {
                return
            }
            const spriteFrame = new cc.SpriteFrame(Ass);
            this.ShowImg.node.active = true;
            this.ShowImg.spriteFrame = spriteFrame;
        });

    }


    Refresh() {
        // console.log("!!!!!!!!!刷新");
        // let flag = this.m_levelNum<=mainRoot.LevelSystem.GetFarLevel(this.m_mode);
        this.isStartGame = false;
        let flag = mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) >= 0 || O1HD.getServices().userSrv.getLevelAllUnlockFlag();
        let flag2 = this.m_levelNum == mainRoot.LevelSystem.GetFarLevel(this.m_mode) //|| (mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum + 1) < 0 && !this.locked);//this.m_levelNum==mainRoot.LevelSystem.GetFarLevel(this.m_mode);
        // console.log(this.m_levelNum, mainRoot.LevelSystem.GetFarLevel(this.m_mode));
        // this.m_numLabel.node.parent.active = flag;
        this.powerNode.active = false;
        if (flag) {
            this.lockBg.active = false;
            this.powerNode.active = true;
            this.levelLayout.active = this.levelObj["infinityFlag"];
            // node_component.loadResourcesPicture(`Img/Page/LevelMenu/${flag2?'farLevel':'unlock'}`,this.node,mainRoot.frameBundle);
        }else{
            this.lockBg.active = true;
            this.powerNode.active = false;
            this.levelLayout.active = false;
        }
        if(flag2){
            this.selcetSp.active = true;
        }
    }
    ClickDown() {
        // this.node.scale = 1.1;
    }

    
    async ClickUp() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        O1HD.getManagers().soundMgr.playVibrate();
        if (!this.isStartGame) {
            // if(this.m_levelNum == mainRoot.LevelSystem.GetFarLevel(this.m_mode)+1){
            if (mainRoot.LevelSystem.GetUnlockLevel(this.m_mode).indexOf(this.m_levelNum) < 0 && !O1HD.getServices().userSrv.getLevelAllUnlockFlag()) {

                const prams:IUnlockPagePrams={
                    mode: this.m_mode,
                    unlockCall:()=>{
                        mainRoot.LevelSystem.ChangeLevel(this.m_levelNum, this.m_mode);
                        this.Refresh();
                        this.lastBtn && this.lastBtn.Refresh();
                        mainRoot.UIModel.showMessageBox(`第${this.m_levelNum}关已解锁`);
                    },
                    isShowOne:true,
                    allCall:()=>{
                        mainRoot.UIModel.showMessageBox(`所有关卡已解锁`);
                        mainRoot.LevelSystem.unAllLock(this.m_mode)
                        O1HD.emit('refrashAll')
                    },
                    cancel:()=>{
                        this.isStartGame = false;
                    }
                }
                mainRoot.UISystem.OpenPage(PAGE.UNLOCKPAGE, prams)
                // modules().uiControlModule.showDialogBox("解锁关卡", "欣赏小视频解锁这一关", true, (ok) => {
                    // if (ok) {
                    //     mainRoot.LevelSystem.ChangeLevel(this.m_levelNum, this.m_mode);
                    //     this.Refresh();
                    //     this.lastBtn && this.lastBtn.Refresh();
                    //     mainRoot.UIModel.showMessageBox(`第${this.m_levelNum}关已解锁`);
                    // } else {
                    //     this.isStartGame = false;
                    // }
                // }, null, "欣赏", "不了", 1, 0, "看视频提前解锁关卡");
                return;
            }
            let needPower:number = this.levelObj["subPower"] || 1;
            if(O1HD.getServices().userSrv.getPower()- needPower < 0){
                mainRoot.UIModel.showUserPropGet(REWARD_TYPE.POWER,gameConfig.ADD_ONCE_POWER);
                return;
            }
            
            this.isStartGame = true;
            // if(this.locked){
            //     mainRoot.UIModel.showMessageBox("请解锁前面的关卡");
            //     return;
            // }
            // mainRoot.LevelSystem.ChangeLevel(this.m_levelNum);
            // console.warn('重置2');
            mainRoot.GotTips = false;
            mainRoot.GotAnswer = false;
            // managers().soundMgr.stopMusic();
            // O1HD.getManagers().soundMgr.playSound('mainSound/inLevel').then();
            await mainRoot.LevelSystem.LoadLevel(mainRoot.LevelSystem.GetSignleLevelInfo(this.m_mode, this.m_levelNum).bundleName, this.m_mode, this.m_levelNum,true,this.levelObj["infinityFlag"] ? this.levelObj : null);
            // console.info("#记录按钮操作");
            services().userSrv.setPower(-needPower,true,true);
            mainRoot.UISystem.ClosePage(PAGE.LEVELMENU);
            mainRoot.UISystem.ClosePage(PAGE.MODEMENU);
            this.scheduleOnce(() => {
                this.isStartGame = false;
            }, 1);
        }
    }
    OnDowning(dt: number) {
    }
}